package com.hayabusa.service.impl;/*
 * @author Hayabusa
 * @date  2021/3/31 14:27
 * @Email:2682557218@qq.com
 */


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hayabusa.mapper.UserMapper;
import com.hayabusa.pojo.User;
import com.hayabusa.service.IUserService;
import org.apache.ibatis.session.SqlSession;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    private SolrClient solrClient;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<User> findInSolr(Integer pageNo,Integer pageSize,String name) {
         List<User> userList =new ArrayList<>();
        try {
            SolrQuery query = new SolrQuery();
            //设定查询字段
            query.set("q", "username_item:"+name);
            //指定返回结果字段
            query.setIncludeScore(true);
            //分页开始页数
            query.setStart(pageNo);
            //设定返回记录数，默认为10条
            query.setRows(pageSize);

            QueryResponse response = solrClient.query(query);
            //获取bean
            //userList = response.getBeans(Object.class);
            SolrDocumentList solrDocumentList = response.getResults();
            System.out.println("查询结果的总记录数：" + solrDocumentList.getNumFound());
            // 第六步：遍历结果并打印。
            for (SolrDocument solrDocument : solrDocumentList) {
                User user=new User();
                user.setId(Integer.valueOf(solrDocument.get("id").toString()) );
                user.setEmail(solrDocument.get("user_email").toString());
                user.setUserName(solrDocument.get("username_item").toString());
                user.setRole(solrDocument.get("role_item").toString());
                user.setPassWord(solrDocument.get("password_item").toString());
                userList.add(user);
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
        return  userList;
        //PageHelper.startPage(pageNo,pageSize);
//        List<User> userList = userMapper.findAll();
//        PageInfo<User> pageInfo = new PageInfo<>(userList);
//        return pageInfo;
    }
    @Override
    public List<User> findAll() {
        return  userMapper.findAll();

    }

    @Override
    public User findByUserName(String username)  {
        return userMapper.findByUserName(username);
    }

    @Override
    public int delById(Integer id) throws Exception {
        int i = userMapper.delById(id);
        int flag=-1;
        if (i>=0){
            solrClient.deleteById(String.valueOf(id));
            solrClient.commit();
            flag=1;
        }
        return flag;
    }

    @Override
    public User selectById(Integer id) {
        return null;
    }


    @Override
    public User getByEmail(String email) {
      return userMapper.getByEmail(email);
    }

    @Override
    public void editById(Integer id, String username, String password) {

    }

    @Override
    public int insertUser(User user) throws Exception{
        int i = userMapper.insertUser(user);
        int flag=-1;
        if (i>=0){
            //同步到索引库
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id",user.getId());
            document.addField("user_email", user.getUserName());
            document.addField("username_item",user.getEmail());
            document.addField("password_item",user.getPassWord());
            document.addField("role_item",user.getRole());
            solrClient.add(document);
            solrClient.commit();
            flag=1;
        }
        return flag;

    }

    @Override
    @Transactional
    public int focusUser( String email) {
        String current_user = (String) redisTemplate.opsForValue().get("current_user");
        redisTemplate.opsForSet().add("follows_"+current_user,email);
        redisTemplate.opsForSet().add("fans_"+email,current_user);
        return 1;
    }

    @Override
    @Transactional
    public int unfocusUser(String email) {
        String current_user = (String) redisTemplate.opsForValue().get("current_user");
        redisTemplate.opsForSet().remove("follows_"+current_user,email);
        redisTemplate.opsForSet().remove("fans_"+email,current_user);
        return 1;
    }
}
